/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package semant.first_pass;

import semant.Env;
import symbol.ClassInfo;
import symbol.MethodInfo;
import symbol.Symbol;
import syntaxtree.MethodDecl;
import syntaxtree.VisitorAdapter;

/**
 *
 * @author Thiago
 */
class MethodHandler extends VisitorAdapter {

    private Env env;
    private ClassInfo info;


    private MethodHandler(Env e, ClassInfo i)
    {
        env = e;

        info = i;
    }

    static void firstPass(Env e, ClassInfo i, MethodDecl node) {

        MethodHandler h = new MethodHandler(e,i);

        node.accept(h);
    }

    public void visit(MethodDecl node)
    {
        Symbol name = Symbol.symbol(node.name.s);

        MethodInfo m = new MethodInfo(node.returnType, name, info.name);
        
        FormalListHandler.firstPass(env, m, node.formals);

        AttributeListHandler.firstPass(env, info, m, node.locals);

        if(!info.addMethod(m))
        {
            MethodInfo old = info.methods.get(name);

            env.err.Error(node.name, new Object[]{"Método \'"+ name + "\' redeclarado para a classe \'" + info.name + "\'",
                                        "Declaração anterior aqui: [" + old.type.line + "," + old.type.row + "]"});
        }

    }

}
